<?php
if('POST' != $_SERVER['REQUEST_METHOD']){
	header('Allow: POST');
	header('HTTP/1.1 405 Method Not Allowed');
	header('Content-Type: text/plain');
	exit();
}
require_once("config.inc.php");
require_once(INCLUDE_PATH . "db.inc.php");
require_once(INCLUDE_PATH . "blog.inc.php");

ob_start();
$blog = new Blog();

$comment_author = trim(strip_tags($_POST['username']));
$comment_author_email = trim(strip_tags($_POST['useremail']));
$comment_author_url = trim(strip_tags($_POST['userwebsite']));
$comment_content = trim(strip_tags($_POST['contents']));

if(isset($_SESSION['User_Name']))
{
	$comment_author = $user_info['F_USER_NICKNAME'];
}

if(!isset($_POST['postid']) or !isset($_GET['ReturnUrl']))
{
	throw new Exception('Please do not fuck my website!');
}

if($comment_author == '' or $comment_author_email == '' or $comment_content == ''){
	throw new Exception('Please innsert values into the input.');
}

if(filter_website($comment_author) or filter_website($comment_author_url) or filter_website($comment_author_email) or filter_website($comment_content)){
	throw new Exception('Excuse, comment function closed!');
}

if(!is_email($comment_author_email)){
	throw new Exception('Please innsert your rigth email.');
}

if($comment_author_url != ''){
	if(!is_url($comment_author_url)){
		throw new Exception('Please innsert your rigth website.');
	}
}

$com_data = array();
$com_data['F_ID_BLOG_INFO'] = 1;
$com_data['F_ID_POSTS_INFO'] = $_POST['postid'];
$user_IP = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
$user_IP = ($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"];
$com_data['F_COMMENTS_USER'] = $comment_author;
$com_data['F_COMMENTS_USER_IP'] = $user_IP;
$com_data['F_COMMENTS_USER_EMAIL'] = $comment_author_email;
$com_data['F_COMMENTS_USER_BLOG'] = $comment_author_url;
$com_data['F_COMMENTS_DATE'] = time();
$com_data['F_COMMENTS_CONTENT'] = $comment_content;

if($blog->insertData('ee_blog_comments', $com_data)){
	$blog->UpdatePostsComments($_POST['postid']);
	header("Location:{$_GET['ReturnUrl']}");
}

/**
 * 校验name
 */
function is_name($str){
	return preg_match("/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/", $str);
}
/**
 * 校验email
 */
function is_email($str){
	return preg_match("/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/", $str);
}
/**
 * 校验网址 
 */
function is_url($str){
	return preg_match("/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"])
*$/", $str);
}
/**
 * 校验内容
 */
function is_contents($str){
	
}
/**
 * 过滤
 */
function filter_website($str){
	return preg_match("/tramadol|dogomania|plastver|world66/", $str);
}
?>